/**
 * Project Name:YCApiServer-common
 * File Name:YcPush.java
 * Package Name:com.yc.music.common.push
 * Date:2016年9月8日下午6:49:30
 * Copyright (c) 2016 
 *
 */
package com.yc.music.common.push;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * ClassName: YcPush <br/>
 * Function: 推送-极光推送封装类. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年9月8日 下午6:49:30 <br/>
 *
 * @author qinhaiguan
 * @version 
 */
public class YcPush {
	
	protected static final Logger logger = LoggerFactory.getLogger(YcPush.class);
	
	public static final String ALERT = "试试";
//	public static final String alias="3733";
//	public static final String content="";
	public static void main(String[] args) {
//		String str=YtConstant.PUSH_TITLE_COMMENT;
		Map<String,Object> param = new HashMap<String,Object>();
//		Map<String,String> map = new HashMap<String,String>();
		param.put("title", "音巢音乐");
		param.put("content", "测试评论消息推送！");
		param.put("type", "comment");
		String pushMessage=JSONObject.toJSONString(param);
		ycSendPushIOS("54426",pushMessage);
//		ycSendPush(pushMessage);
	}
	
	/**
	 * 
	 * ycSendPushAndroid:(针对别名推送). <br/> 
	 *
	 * @param alias  别名
	 * @param content 消息内容
	 * @return  
	 * @since JDK 1.7
	 */
	public static PushResult ycSendPushAndroid(String alias,String content) {
	    // HttpProxy proxy = new HttpProxy("localhost", 3128);
	    // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
//		ClientConfig clientConfig = ClientConfig.getInstance();
//      JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
		JPushClient ycPush=InitYcPush.getYcPushInstance();
        // For push, all you need do is to build PushPayload object.
		logger.info("推送别名消息给极光内容android:"+content);
        PushPayload payload=pushObject_android_alias_Message(alias,content);
        payload.resetOptionsTimeToLive(1); //设置离校消息保留时长为1天
        
        PushResult result=null;
        
        try {
            result = ycPush.sendPush(payload);
//            System.out.println(">>>>>>>>>"+result);
//            String msg_id=JSONObject.parseObject(JSONObject.toJSONString(result)).getString("msg_id");
//            System.out.println(">>>>>>>>>msg_id:"+msg_id);
            logger.info("推送别名消息给android返回结果:" + result);
            
        } catch (APIConnectionException e) {
        	logger.error("推送别名消息android异常Connection error. Should retry later. ", e);
            
        } catch (APIRequestException e) {
        	logger.error("推送别名消息android异常Error response from JPush server. Should review and fix it. ", e);
        	logger.info("推送别名消息android异常HTTP Status: " + e.getStatus());
        	logger.info("推送别名消息android异常Error Code: " + e.getErrorCode());
        	logger.info("推送别名消息android异常Error Message: " + e.getErrorMessage());
        	logger.info("推送别名消息android异常Msg ID: " + e.getMsgId());
        }
        return result;
	}
	

	/**
	 * 
	 * ycSendPushIOS:(针对别名推送). <br/> 
	 *
	 * @param alias  别名
	 * @param content 消息内容
	 * @return  
	 * @since JDK 1.7
	 */
	public static PushResult ycSendPushIOS(String alias,String content) {
	   
		JPushClient ycPush=InitYcPush.getYcPushInstance();
		logger.info("推送别名消息给极光内容IOS:"+content);
        PushPayload payload=pushObject_ios_alias_Message(alias,JSONObject.parseObject(content).getString("content"),
        		JSONObject.parseObject(content).getString("type"),JSONObject.parseObject(content).getString("orderNo"));
        payload.resetOptionsTimeToLive(1); //设置离校消息保留时长为1天
        
        PushResult result=null;
        
        try {
            result = ycPush.sendPush(payload);
//            System.out.println(">>>>>>>>>"+result);
//            String msg_id=JSONObject.parseObject(JSONObject.toJSONString(result)).getString("msg_id");
//            System.out.println(">>>>>>>>>msg_id:"+msg_id);
            logger.info("推送别名消息给IOS返回结果:" + result);
            
        } catch (APIConnectionException e) {
        	logger.error("推送IOS异常Connection error. Should retry later. ", e);
            
        } catch (APIRequestException e) {
        	logger.error("推送别名消息IOS异常Error response from JPush server. Should review and fix it. ", e);
        	logger.info("推送别名消息IOS异常HTTP Status: " + e.getStatus());
        	logger.info("推送别名消息IOS异常Error Code: " + e.getErrorCode());
        	logger.info("推送别名消息IOS异常Error Message: " + e.getErrorMessage());
        	logger.info("推送别名消息IOS异常Msg ID: " + e.getMsgId());
        }
        return result;
	}
	
	
	public static PushResult ycSendPushCooperateIOS(String alias,String content) {
		   
		JPushClient ycPush=InitYcPush.getYcPushInstance();
		logger.info("推送别名消息给极光内容IOS:"+content);
        PushPayload payload=pushObject_ios_alias_Message(alias,JSONObject.parseObject(content).getString("content"),
        		JSONObject.parseObject(content).getString("type"),JSONObject.parseObject(content).getString("orderNo"),JSONObject.parseObject(content).getString("did"));
        payload.resetOptionsTimeToLive(1); //设置离校消息保留时长为1天
        
        PushResult result=null;
        
        try {
            result = ycPush.sendPush(payload);
//            System.out.println(">>>>>>>>>"+result);
//            String msg_id=JSONObject.parseObject(JSONObject.toJSONString(result)).getString("msg_id");
//            System.out.println(">>>>>>>>>msg_id:"+msg_id);
            logger.info("推送别名消息给IOS返回结果:" + result);
            
        } catch (APIConnectionException e) {
        	logger.error("推送IOS异常Connection error. Should retry later. ", e);
            
        } catch (APIRequestException e) {
        	logger.error("推送别名消息IOS异常Error response from JPush server. Should review and fix it. ", e);
        	logger.info("推送别名消息IOS异常HTTP Status: " + e.getStatus());
        	logger.info("推送别名消息IOS异常Error Code: " + e.getErrorCode());
        	logger.info("推送别名消息IOS异常Error Message: " + e.getErrorMessage());
        	logger.info("推送别名消息IOS异常Msg ID: " + e.getMsgId());
        }
        return result;
	}
	
	
	/**
	 * 
	 * ycSendPushAndroid:(针对android). <br/> 
	 *
	 * @param content  消息内容
	 * @return
	 * @since JDK 1.7
	 */
	public static PushResult ycSendPushAndroid(String content) {
	   
		JPushClient ycPush=InitYcPush.getYcPushInstance();
		logger.info("推送给极光内容android:"+content);
        PushPayload payload=pushObject_android_Message(content);
        payload.resetOptionsTimeToLive(1); //设置离校消息保留时长为1天
        PushResult result=null;
        try {
        	result = ycPush.sendPush(payload);
        	logger.info("推送android返回结果 : " + result);
        } catch (APIConnectionException e) {
        	logger.error("推送android异常Connection error. Should retry later. ", e);
            
        } catch (APIRequestException e) {
        	logger.error("推送android异常Error response from JPush server. Should review and fix it. ", e);
        	logger.info("推送android异常HTTP Status: " + e.getStatus());
        	logger.info("推送android异常Error Code: " + e.getErrorCode());
        	logger.info("推送android异常Error Message: " + e.getErrorMessage());
        	logger.info("推送android异常Msg ID: " + e.getMsgId());
        }
       return result;
	}
	
	
	/**
	 * 
	 * ycSendPushIOS:(针对ios). <br/> 
	 *
	 * @param content  消息内容
	 * @return
	 * @since JDK 1.7
	 */
	public static PushResult ycSendPushIOS(String content) {
	   
		JPushClient ycPush=InitYcPush.getYcPushInstance();
		logger.info("推送给极光内容IOS:"+content);
        PushPayload payload=pushObject_ios_Message(JSONObject.parseObject(content).getString("content"),
        		JSONObject.parseObject(content).getString("type"));
        payload.resetOptionsTimeToLive(1); //设置离校消息保留时长为1天
        PushResult result=null;
        try {
        	result = ycPush.sendPush(payload);
        	logger.info("推送IOS返回结果 : " + result);
        } catch (APIConnectionException e) {
        	logger.error("推送IOS异常Connection error. Should retry later. ", e);
            
        } catch (APIRequestException e) {
        	logger.error("推送IOS异常Error response from JPush server. Should review and fix it. ", e);
        	logger.info("推送IOS异常HTTP Status: " + e.getStatus());
        	logger.info("推送IOS异常Error Code: " + e.getErrorCode());
        	logger.info("推送IOS异常Error Message: " + e.getErrorMessage());
        	logger.info("推送IOS异常Msg ID: " + e.getMsgId());
        }
       return result;
	}
	

	
	
	
	
	/**
	 * 
	 * pushObject_android_alias_Message:通过别名给android的平台推送自定义消息内容. <br/> 
	 *
	 * @param alias  客户端绑定别名
	 * @return
	 * @since JDK 1.7
	 */
	public static PushPayload pushObject_android_alias_Message(String alias,String content) { 
		
        return PushPayload.newBuilder() 
                .setPlatform(Platform.android()) //推送给android
        		.setAudience(Audience.alias(alias))
                .setMessage(Message.content(content))
                .build();
	}
	
	
	/**
	 * 
	 * pushObject_android_Message:(自定义消息内容消息推送给android的平台). <br/> 
	 * @author qinhaiguan
	 * @param content  
	 * @return
	 * @since JDK 1.7
	 */
	public static PushPayload pushObject_android_Message(String content) {
		
        return PushPayload.newBuilder()
                .setPlatform(Platform.android()) //推送给android平台
        		.setAudience(Audience.all())
                .setMessage(Message.content(content))
                .build();
	}
	
	
	/**
	 * 
	 * pushObject_ios_alias_Message:通过别名给IOS的平台推送自定义消息内容. <br/> 
	 *
	 * @param alias  客户端绑定别名
	 * @return
	 * @since JDK 1.7
	 */
	public static PushPayload pushObject_ios_alias_Message(String alias,String content,String type,String orderNo) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())  //推送给IOS
        		.setAudience(Audience.alias(alias))
        		.setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(content)
                                .addExtra("type", type)
                                .addExtra("orderNo", orderNo)
                                .build())
                        .build())
                .setMessage(Message.content(content))
                .build();
	}
	
	
	public static PushPayload pushObject_ios_alias_Message(String alias,String content,String type,String orderNo,String did) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())  //推送给IOS
        		.setAudience(Audience.alias(alias))
        		.setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(content)
                                .addExtra("type", type)
                                .addExtra("orderNo", orderNo)
                                .addExtra("did", did)
                                .build())
                        .build())
                .setMessage(Message.content(content))
                .build();
	}
	
	/**
	 * 
	 * pushObject_ios_Message:(自定义消息内容消息推送给IOS的平台). <br/> 
	 * @author qinhaiguan
	 * @param content  
	 * @return
	 * @since JDK 1.7
	 */
	public static PushPayload pushObject_ios_Message(String content,String type) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios()) //推送给IOS
        		.setAudience(Audience.all())
        		.setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(content)
                                .addExtra("type", type)
                                .build())
                        .build())
                .setMessage(Message.content(content))
                .build();
	}
	
}
